package br.com.felix.dbm.ui.components;

import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JButton;

public class DeleteJButton extends JButton {

	private static final long serialVersionUID = 3731810583048666862L;
	private DeleteMouseListener deleteMouseListener;
	
	private boolean isInConfirmationState = false;
	private String confirmationText = "now!";
	private String normalText;
	private Color confirmationColor = Color.RED;
	private Color normalColor;
	
	public DeleteJButton() {
		super();
	}
	public DeleteJButton(Action a) {
		super(a);
	}
	public DeleteJButton(Icon icon) {
		super(icon);
	}
	public DeleteJButton(String text, Icon icon) {
		super(text, icon);
	}
	public DeleteJButton(String text) {
		super(text);
	}
	
	
	{
		deleteMouseListener = new DeleteMouseListener();
		super.addMouseListener(deleteMouseListener);
	}
	
	
	
	public String getConfirmationText() {
		return confirmationText;
	}
	public void setConfirmationText(String confirmationText) {
		this.confirmationText = confirmationText;
	}
	
	
	
	class DeleteMouseListener implements MouseListener{
		private List<MouseListener> lstMouseListeners = new ArrayList<MouseListener>();
		public void addMouseListener(MouseListener ml){
			lstMouseListeners.add(ml);
		}
		
		@Override
		public void mouseClicked(MouseEvent e) {
			if(isInConfirmationState)
			for(MouseListener ml:lstMouseListeners)
				ml.mouseClicked(e);
		}

		@Override
		public void mouseEntered(MouseEvent e) {
			if(isInConfirmationState)
			for(MouseListener ml:lstMouseListeners)
				ml.mouseEntered(e);
		}

		@Override
		public void mouseExited(MouseEvent e) {
			if(isInConfirmationState)
			for(MouseListener ml:lstMouseListeners)
				ml.mouseExited(e);		}

		@Override
		public void mousePressed(MouseEvent e) {
			if(isInConfirmationState)
			for(MouseListener ml:lstMouseListeners)
				ml.mousePressed(e);
		}

		@Override
		public void mouseReleased(MouseEvent e) {
			if(isInConfirmationState){
				for(MouseListener ml:lstMouseListeners){
					ml.mouseReleased(e);
				}
			}
			else{
				setInConfirmationState(true);
			}
		}
	}
	
	
	private synchronized void setInConfirmationState(boolean confirmationState){
		isInConfirmationState = confirmationState;
		if(confirmationState){
			Thread switchBack = new Thread(){
				@Override
				public void run() {
					try {
						sleep(2000);
						setInConfirmationState(false);
					} 
					catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			};
			normalText = getText();
			normalColor = getBackground();
			setText(confirmationText);
			setBackground(confirmationColor);
			validate();
			switchBack.start();
		}
		else{
			setText(normalText);
			setBackground(normalColor);
		}
		
	}
	
	
	
	public Color getConfirmationColor() {
		return confirmationColor;
	}
	public void setConfirmationColor(Color confirmationColor) {
		this.confirmationColor = confirmationColor;
	}

	
	@Override
	public synchronized void addMouseListener(MouseListener ml) {
		if(deleteMouseListener==null){
			super.addMouseListener(ml);
		}
		else{
			deleteMouseListener.addMouseListener(ml);
		}
	}
	
}
